

package devaluator.plugin.view;


import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.core.internal.resources.Project;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer; 
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.model.WorkbenchLabelProvider;


import devaluator.functionality.Analyzer;
import devaluator.functionality.ClassAnalyzer;
import devaluator.functionality.Classparser;
import devaluator.functionality.Parser;
import devaluator.functionality.GetProjectName;
import devaluator.functionality.GetProjectPath;



import java.io.File;
import java.util.ArrayList;


import org.eclipse.ui.part.ViewPart;




public class Devaluator extends ViewPart {

	ListViewer viewer;
	Label lable;
	String projectPath=null;
	ArrayList<String> alist = new ArrayList<String>();
	GetProjectName pName = new GetProjectName();
			
	//The constructor for Devaluator
	public Devaluator() {
		// TODO Auto-generated constructor stub
	}

	// the listener we register with the selection service 
			private ISelectionListener listener = new ISelectionListener() {
				public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection) {
					// we ignore our own selections
					if (sourcepart != Devaluator.this) {
						setContentDescription(sourcepart.getTitle() + " (" + selection.getClass().getName() + ")");
						if (selection instanceof IStructuredSelection) {
							
							IStructuredSelection ss = (IStructuredSelection) selection;
							Object element = ss.getFirstElement();
							projectPath = pName.match(element.toString());
							projectPath = GetProjectPath.getDir(projectPath);
							
						}
					}
					if (projectPath == null)
						projectPath = "No project selected";
				}
	};

	
	
	/* 
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
	public void createPartControl(Composite parent) {
	
		viewer = new ListViewer(parent);
		getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(listener);
		setOutput(4); 
       				
	}

	
	//Passing the focus request to the viewer's control.
	public void setFocus() {
		viewer.getControl().setFocus();

	}
	
	public void setOutput(int option) {
		if (projectPath == null)
			projectPath = "No project selected";
		File temFile = new File(projectPath);
		if (temFile.exists()){
	    Parser test = new Classparser(temFile);
	    Analyzer x = new ClassAnalyzer();
	    
	    ArrayList<ArrayList<String>> y = x.process(test.match()); 
    	viewer.setContentProvider(new ArrayContentProvider());
    	viewer.setLabelProvider(new LabelProvider());
    	
	    if(option==2){
	    	
	    	viewer.setInput(y.get(2));
	    }
	    
	    if(option==1){
		   
	        viewer.setInput(y.get(1));
	        
		}
	    if(option==0){
		  
	        viewer.setInput(y.get(0));
		 }
		    
		if(option==3){
			alist.add(projectPath);
			viewer.setInput(alist);
		        
		}
		if(option==4)
		{
			//lable.setText("Please select a option from top right!");
			
		}

	}
	}
	
	public void dispose() {
		// important: We need do unregister our listener when the view is disposed
		getSite().getWorkbenchWindow().getSelectionService().removeSelectionListener(listener);
		super.dispose();
	}
}
